#!/usr/bin/python
#coding=utf8

import requests

class WeatherUtil(object):

    """
    获得天气信息
    :param city: 城市名称/城市id/城市经纬度/城市ip
    :return: 天气信息
    """
    def get_weather(self, city):
        data = self._request_weather(city)
        if None == data:
            return

        city, now, suggestion = self._parse_data(data)
        result = '时间: %s\n' \
                 '城市: %s\n' \
                 '天气状况: %s\n' \
                 '实时温度: %s℃\n' \
                 '体感温度: %s℃\n' \
                 '相对湿度: %s%%\n' \
                 '舒适度指数: %s %s\n\n' \
                 '穿衣指数: %s %s\n\n' \
                 '感冒指数: %s %s\n\n' \
                 '运动指数: %s %s\n\n' \
                 '紫外线指数: %s %s\n\n' \
                 '' % (
            now['date'],
            city['name'],
            now['cond'],
            now['temperature'],
            now['fl'],
            now['humidity'],
            suggestion['comf']['brf'], suggestion['comf']['txt'],
            suggestion['drsg']['brf'], suggestion['drsg']['txt'],
            suggestion['flu']['brf'], suggestion['flu']['txt'],
            suggestion['sport']['brf'], suggestion['sport']['txt'],
            suggestion['uv']['brf'], suggestion['uv']['txt']
        )
        return result


    """
    获得 app_key
    :return: app_key
    """
    def _get_app_key(self):
        return '39613441058548b09e3f160e446e0d7b'


    """
    请求获得天气信息
    :param city: 城市名称/城市id/城市经纬度/城市ip
    :return: 请求的响应信息的主要信息
    """
    def _request_weather(self, city):
        if None == city or 0 == len(city):
            return
        try:
            url = 'https://free-api.heweather.com/v5/weather?city=%s&key=%s' % (city, self._get_app_key())
            res = requests.get(url).json()
            data = res.get('HeWeather5')[0]
            if 'ok' == data.get('status'):
                return data
        except:
            return


    """
    解析响应信息
    :param res: 响应信息
    :return: 天气信息
    """
    def _parse_data(self, data):

        # 城市信息
        city = self._parse_city_info(data)
        now = self._parse_now_weather(data)
        suggestion = self._parse_suggestion_info(data)
        return (city, now, suggestion)


    """
    解析响应信息中的城市信息
    :param res: 响应信息
    :return: 城市信息
    """
    def _parse_city_info(self, data):
        basic = data.get('basic')
        city = {}
        city['id'] = basic.get('id')            # id
        city['name'] = basic.get('city')        # 名称
        city['country'] = basic.get('cnty')     # 国家
        city['longitude'] = basic.get('lon')    # 经度
        city['latitude'] = basic.get('lat')     # 纬度
        return city


    def _parse_weather_dict(self, weather_dict):
        weather = {}
        weather['date'] = weather_dict.get('date')          # 日期
        weather['fl'] = weather_dict.get('fl')              # 体感温度
        weather['humidity'] = weather_dict.get('hum')       # 相对湿度
        weather['pcpn'] = weather_dict.get('pcpn')          # 降水量
        weather['pop'] = weather_dict.get('pop')            # 降水概率
        weather['pressure'] = weather_dict.get('pres')      # 气压
        weather['temperature'] = weather_dict.get('tmp')    # 温度
        weather['visibility'] = weather_dict.get('vis')     # 能见度
        weather['cond'] = weather_dict.get('cond').get('txt')   # 天气状况


    def _parse_now_weather(self, data):
        now = data.get('now')
        basic = data.get('basic')
        weather = {}
        weather['date'] = basic.get('update').get('loc')    # 日期
        weather['cond'] = now.get('cond').get('txt')        # 天气状况
        weather['temperature'] = now.get('tmp')    # 温度

        weather['fl'] = now.get('fl')              # 体感温度
        weather['humidity'] = now.get('hum')       # 相对湿度
        weather['pcpn'] = now.get('pcpn')          # 降水量
        weather['pop'] = now.get('pop')            # 降水概率
        weather['pressure'] = now.get('pres')      # 气压
        weather['visibility'] = now.get('vis')     # 能见度
        return weather


    def _parse_suggestion_info(self, data):
        suggestion = data.get('suggestion')
        return suggestion

util = WeatherUtil()
print(util.get_weather("苏州"))